package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.HotelInfoLogic;
import com.hotel.BL.RoomLogic;

import data.HotelInfo;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class HotelInfoService extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String doClass = req.getParameter("func");
			if (doClass.equals("saveHotelInfo")) {
				SaveHotelInfo(req, resp);
			}else if (doClass.equals("getHotelInfo")) {
				GetHotelInfo(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void SaveHotelInfo(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		 long hotelId = (Long) session.getAttribute("hotelId");
		
		 String name = req.getParameter("name");
		 String address = req.getParameter("address");
		 String address2 =req.getParameter("address2");
		 String country =req.getParameter("country");
		 String state =req.getParameter("state");
		 String city =req.getParameter("city");
		 String zipcode =req.getParameter("zipcode");
		 String phone =req.getParameter("phone");
		 String fax =req.getParameter("fax");
		 String logo =req.getParameter("logo");
		 String photo =req.getParameter("photo");
		 String website =req.getParameter("website");
		 String vatId =req.getParameter("vatId");
		 String description =req.getParameter("description");
		 String bAddress1 =req.getParameter("bAddress1");
		 String bAddress2 =req.getParameter("bAddress2");
		 String country2 =req.getParameter("country2");
		 String state2 =req.getParameter("state2");
		 String bCity =req.getParameter("bCity");
		 String bZipcode =req.getParameter("bZipcode");
		 String bPhone =req.getParameter("bPhone");
		 String bFax =req.getParameter("bFax");
		 String cSal =req.getParameter("cSal");
		 String cName =req.getParameter("cName");
		 String cLastname =req.getParameter("cLastname");
		 String cDesign =req.getParameter("cDesign");
		 String cPhone =req.getParameter("cPhone");
		 String cExtn =req.getParameter("cExtn");
		 String cFax =req.getParameter("cFax");
		 String cEmail =req.getParameter("cEmail");
		 String cMobile = req.getParameter("cMobile");
		 String email =req.getParameter("email");
		 String offered = req.getParameter("offered");

		 HotelInfo hotelinfo = new HotelInfoLogic().GetHotelInfoByHotelId(hotelId);
		 if(hotelinfo == null){
			 hotelinfo = new HotelInfoLogic().AddHotelInfo(hotelId,name,address,address2,country,state,city,zipcode,
						phone,fax,logo,photo,website,vatId,description,bAddress1,bAddress2,country2,state2,
						bCity,bZipcode,bPhone,bFax,cSal,cName,cLastname,cDesign,cPhone,cExtn,cFax,cEmail,cMobile,email,offered);
		 }else{
			 hotelinfo = new HotelInfoLogic().EditHotelInfo(hotelinfo.getId(),name,address,address2,country,state,city,zipcode,
						phone,fax,logo,photo,website,vatId,description,bAddress1,bAddress2,country2,state2,
						bCity,bZipcode,bPhone,bFax,cSal,cName,cLastname,cDesign,cPhone,cExtn,cFax,cEmail,cMobile,email,offered);
		 }
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(hotelinfo));
	}

	private void GetHotelInfo(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session = req.getSession(true);
		 long hotelId = (Long) session.getAttribute("hotelId");
		 HotelInfo hotelinfo = new HotelInfoLogic().GetHotelInfoByHotelId(hotelId);
		 int roomamount = new RoomLogic(req,resp).GetAllRoom(hotelId).size();
		 Gson gson = new Gson();
		 
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("roomamount",roomamount);
		data.put("data", hotelinfo);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}

}
